----------------------------
-- Simplistic Street
-- by Jammie Dodger
--
-- Project Started - October 20, 2009
-- Completion -None
----------------------------

timer.Simple(0, function() LocalPlayer().Inventory = LocalPlayer().Inventory or {} end)


local function SetInventoryItems(um)
	local NumberOfItems = um:ReadChar()
	 for i = 1, NumberOfItems do
		local Item = um:ReadString()
		local Quantity = um:ReadShort()
		timer.Simple(0, function() LocalPlayer().Inventory[Item] = Quantity end)
	end        
end

usermessage.Hook("SetItems", SetInventoryItems)

local y = ScrH()-400

function Inventory()

		InventoryFrame = vgui.Create( "DFrame" )
		InventoryFrame:SetSize( 500, 250 )
		InventoryFrame:SetPos( ScrW(), y )
		InventoryFrame:SetTitle( "Inventory" )
		InventoryFrame:SetDraggable( false )
		InventoryFrame:ShowCloseButton( false )
	--	InventoryFrame:MakePopup()

		local DermaList = vgui.Create( "DPanelList", InventoryFrame )
		DermaList:SetPos( 10, 32 )
		DermaList:SetSize( InventoryFrame:GetWide() - 20, InventoryFrame:GetTall() - 42 )
		DermaList:SetSpacing( 3 )
		DermaList:EnableHorizontal( true )
		DermaList:EnableVerticalScrollbar( true )
		
	for k, v in pairs( LocalPlayer().Inventory ) do
	
		local Info = Items[k]

		if v != 0 then
		
		local Icon = vgui.Create( "SpawnIcon", DermaList )
		Icon:SetPos( 10, 10 )
		Icon:SetSize( 60, 60 )
		Icon:SetModel(Info.Model or "models/error.mdl" )
		Icon:SetToolTip(Info.Name.."\n\n"..Info.Description)
		Icon.OnMousePressed = function()
			Options = DermaMenu()
			Options:AddOption( "Drop", function() RunConsoleCommand( "ss_dropitem", k ) end )
			
			if Info.Useable then
				Options:AddOption( "Use", function() RunConsoleCommand( "ss_useitem", k ) end )
			end
			
			Options:SetParent( Icon )
			Options:Open()			
			
		end
	
			
		local amt = vgui.Create( "DLabel", Icon )
		amt:SetText( v )
		amt:SizeToContents()
		amt:SetPos( 55, 50 )
		
		DermaList:AddItem( Icon )
		end
	end
end

function GM:OnContextMenuOpen()
	if !InventoryFrame then Inventory() end
	InventoryFrame:MoveTo(ScrW()-500,y,0.2,0,1)
	InventoryFrame:SetVisible(true)
	gui.EnableScreenClicker(true)
end

function GM:OnContextMenuClose()
	gui.EnableScreenClicker(false)
	InventoryFrame:MoveTo(ScrW(),y,0.2,0,1)
	timer.Simple(0.3, function() InventoryFrame:Remove() Inventory() end)
end


--[[local function SetInventoryItems(um)
	local NumberOfItems = um:ReadChar()
	print("Recieved number: "..NumberOfItems)
	 for i = 1, NumberOfItems do
		local Item = um:ReadString()
		local Quantity = um:ReadShort()
		print("Item: "..Item..", Quantity: "..Quantity)
		timer.Simple(0, function() LocalPlayer().Inventory[Item] = Quantity end)
	end        
end

usermessage.Hook("SetItems", SetInventoryItems)

local inv
local function ShowInv(pl,cmd,args)
	if !ValidPanel(inv) then inv = vgui.Create("Inventory") end
	inv:SetVisible(true)
	inv:InvalidateLayout()
	local y = ScrH()-400
	if !inv.Open then 
		inv:MoveTo(ScrW()-500,y,0.2,0,1)
		gui.EnableScreenClicker(true)
	else
		inv:MoveTo(ScrW(),y,0.2,0,1)
		gui.EnableScreenClicker(false)
	end
	inv.Open = !inv.Open
end
concommand.Add("rp_inventory",ShowInv)

local INVENTORY = {}

function INVENTORY:Init()
	self:SetSize(500,280)
	self:SetPos(ScrW(),ScrH()-400)
	self:SetTitle("Inventory")
	self:SetDraggable(false)
	self:ShowCloseButton(false)
	self.List = vgui.Create("DPanelList", self)
	self.List:StretchToParent(5,35,5,5);
	self.List:EnableHorizontal(true);
	self.List:EnableVerticalScrollbar()
	self.Open = false
	self.animPress = Derma_Anim( "Popout", self, self.PressedAnim )
	self.animPress2 = Derma_Anim( "Popin", self, self.PressedAnim2 )
	for k,v in pairs(LocalPlayer().Inventory) do
		print(k, v)
		self:MakeItem(k,v)
	end
end

function INVENTORY:MakeItem(Key, Value)

	local Info = Items[Key]

	local panel = vgui.Create("DModelPanel")
	panel:SetModel(Info.Model)
	panel:SetTooltip("") 
	panel:SetSize(80, 80)
	panel:SetCamPos(Info.Camera)
	panel:SetLookAt(Info.LookAt)
	panel.PaintOver = function() draw.SimpleText(Value,"ScoreboardSub",60,60,Color(255,255,255,255),2,4) end
	panel.OnMousePressed = 
	function()
		local menu = DermaMenu()
		if Info.Useable then
			menu:AddOption("Use",function() RunConsoleCommand("ss_useitem", Key) end)
		end
		menu:AddOption("Drop",function() RunConsoleCommand("ss_dropitem", Key) end)
		menu:Open()
	end
	self.List:AddItem(panel)
end

vgui.Register("Inventory",INVENTORY,"DFrame")

hook.Add("InitPostEntity","CreateInventory",function() LocalPlayer().Inventory = LocalPlayer().Inventory or {} inv = vgui.Create("Inventory") end)
	
function GetInventoryPanel()
	return inv
end--]]